from threading import Timer
from datetime import datetime
import os
from openpyxl import load_workbook,Workbook


class MyTimer( object ):

    def __init__( self, start_time, interval, callback_proc, args=None, kwargs=None ):

        self.__timer = None
        self.__start_time = start_time
        self.__interval = interval
        self.__callback_pro = callback_proc
        self.__args = args if args is not None else []
        self.__kwargs = kwargs if kwargs is not None else {}

    def exec_callback( self, args=None, kwargs=None ):
        self.__callback_pro( *self.__args, **self.__kwargs )
        self.__timer = Timer( self.__interval, self.exec_callback )
        self.__timer.start()

    def start( self ):
        interval = self.__interval - ( datetime.now().timestamp() - self.__start_time.timestamp() )
        # print( interval )
        self.__timer = Timer( interval, self.exec_callback )
        self.__timer.start()

    def cancel( self ):
        self.__timer.cancel()
        self.__timer = None

class AA:
    linenum = 0
    def hello( self, fileName ):
        print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
        command = "adb shell dumpsys meminfo com.oppo.quicksearchbox" #可以直接在命令行中执行的命令
        r = os.popen(command) #执行该命令
        info = r.readlines() #读取命令行的输出到一个list
        self.linenum = self.linenum + 1
        for line in info: #按行遍历
            line = line.strip('\r\n')
            iswrite = 0
            rowNum = -1
            if line.find("TOTAL:")>=0:
                rowNum = 0
                start = line.find('TOTAL:')
                end = line.find('TOTAL SWAP PSS:')
                line = line[start:end]
                print(line)
                line = line.replace("TOTAL:", "")
                line = line.replace(" ", "")
            if line.find("Java Heap:")>=0:
                rowNum = 1
                print(line)
                line = line.replace("Java Heap:", "")
                line = line.replace(" ", "")
            if line.find("Native Heap:")>=0:
                rowNum = 2
                print(line)
                line = line.replace("Native Heap:", "")
                line = line.replace(" ", "")
            if line.find("Code:")>=0:
                rowNum = 3
                print(line)
                line = line.replace("Code:", "")
                line = line.replace(" ", "")
            if line.find("Stack:")>=0:
                rowNum = 4
                print(line)
                line = line.replace("Stack:", "")
                line = line.replace(" ", "")
            if line.find("Graphics:")>=0:
                rowNum = 5
                print(line)
                line = line.replace("Graphics:", "")
                line = line.replace(" ", "")
            if line.find("Private Other:")>=0:
                rowNum = 6
                print(line)
                line = line.replace("Private Other:", "")
                line = line.replace(" ", "")
            if line.find("System:")>=0:
                rowNum = 7
                print(line)
                line = line.replace("System:", "")
                line = line.replace(" ", "")
            if line.find(" Views:")>=0:
                rowNum = 8
                start = line.find('Views:')
                end = line.find('ViewRootImpl:')
                line = line[start:end]
                print(line)
                line = line.replace("Views:", "")
                line = line.replace(" ", "")
            if line.find("Activities:")>=0:
                rowNum = 9
                start = line.find('Activities:')
                line = line[start:]
                print(line)
                line = line.replace("Activities:", "")
                line = line.replace(" ", "")

            if rowNum != -1:
                try:
                    # 尝试打开文件
                    rb = load_workbook(fileName)
                except Exception:
                    # 如果没有文件就进行创建一个文件
                    nb = Workbook()
                    # 创建一个sheet
                    nb.create_sheet('性能数据',0)
                    nbs = nb.worksheets[0]
                    # 创建表头
                    list1 = ['TOTAL','Java Heap','Native Heap','Code','Stack','Graphics','Private Other','System','Views','Activities']
                    for i in range(len(list1)):
                        nbs.cell(1,i+1,list1[i])
                    # 保存文件
                    nb.save(fileName)
                    rb = load_workbook(fileName)
                ws = rb.worksheets[0]
                ws.cell(self.linenum+1,rowNum+1,line)
                rb.save(fileName)




if __name__ == "__main__":
    aa = AA()
    start = datetime.now()
    fileName=os.path.join(os.path.dirname(os.path.abspath(__file__)),'pythontest1.xlsx')
    tmr = MyTimer( start, 3, aa.hello, [ fileName] )
    tmr.start()
    #time.sleep(18)
    #tmr.cancel()
